<?php

namespace App\Http\Controllers\Auth;

use App\Models\AdminRole;
use App\Models\Nav;
use App\Models\Role;
use App\Models\RoleNav;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use mysql_xdevapi\Exception;
use Symfony\Component\HttpFoundation\Session\Attribute\NamespacedAttributeBag;

class
RoleController extends Controller
{
    /** 角色 */
    public function index(Request $request)
    {
        if($request->isMethod('post')){
            $model = Role::paginate(config('base.limit'));

            return $this->data($model->items(),$model->total());
        }
        return view('auth.role.index');
    }

    /** 添加角色 */
    public function edit(Request $request)
    {
        $id = $request->get('id');

        if(!empty($id)){
            $model = Role::find($id);
        }else{
            $model = new Role();
        }

        if($request->isMethod('post')){
            $data = $request->all();

            /** 基础验证 */
            $validator = $this->validateRole($data);
            if ($validator->fails()) {
                return $this->fail($validator->errors()->first());
            }

            $nav_ids = Nav::parentsAll($data['role_nav_id']);

            $model->name = $data['name'];
            $model->description = $data['description'];
            $model->access = json_encode($nav_ids);
            if($model->save()){
                return $this->success('操作成功!');
            }

        }
        $nav = Nav::lists($type=2,false,'tree');
        $access = $model->access??"{}";
        $nav = $this->authTree($nav,$access);

        return view('auth.role.edit',['nav'=>$nav,'model'=>$model]);
    }

//    /** 权限分配 */
//    public function access(Request $request){}

    /** 删除角色 */
    public function del(Request $request)
    {
        $id = $request->input('id');
        DB::table('roles')->where(['id'=>$id])->delete();
        return $this->success('删除成功');
    }

    protected function validateRole($data){
        $validator = Validator::make($data, [
            'name'          => 'required|max:50',
            'description'   => 'required|max:255',
        ], [
            'required'      => ':attribute不得为空',
            'max'           => ':attribute不得超过:max',
        ], [
            'name'          => '角色名称',
            'description'   => '角色描述',
        ]);

        return $validator;
    }

    /** authTree格式化 */
    private function authTree($data,$access)
    {
        $tree = array();
        foreach($data as $key=>$item){
            $arr['id'] = $item['id'];
            $arr['name'] = $item['name'];
            $arr['checked']  = in_array($item['id'],json_decode($access,true))?true:false;
            $arr['disabled']  = false;

            if(!empty($item['child'])){
                $arr['child'] = $this->authTree($item['child'],$access);
            }
            $tree[] = $arr;
        }
        return $tree;
    }
}
